|
1
|
|
|
import merge from "../help/merge.js"; |
|
2
|
|
|
import {calculateEncodingAttributes, getTotalWidthOfEncodings, getMaximumHeightOfEncodings} from "./shared.js"; |
|
3
|
|
|
|
|
4
|
|
|
var svgns = "http://www.w3.org/2000/svg"; |
|
5
|
|
|
|
|
6
|
|
|
class SVGRenderer{ |
|
7
|
|
|
constructor(svg, encodings, options){ |
|
8
|
|
|
this.svg = svg; |
|
9
|
|
|
this.encodings = encodings; |
|
10
|
|
|
this.options = options; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
render(){ |
|
14
|
|
|
var currentX = this.options.marginLeft; |
|
15
|
|
|
|
|
16
|
|
|
this.prepareSVG(); |
|
17
|
|
|
for(let i = 0; i < this.encodings.length; i++){ |
|
18
|
|
|
var encoding = this.encodings[i]; |
|
19
|
|
|
var encodingOptions = merge(this.options, encoding.options); |
|
20
|
|
|
|
|
21
|
|
|
var group = createGroup(currentX, encodingOptions.marginTop, this.svg); |
|
22
|
|
|
|
|
23
|
|
|
setGroupOptions(group, encodingOptions); |
|
24
|
|
|
|
|
25
|
|
|
this.drawSvgBarcode(group, encodingOptions, encoding); |
|
26
|
|
|
this.drawSVGText(group, encodingOptions, encoding); |
|
27
|
|
|
|
|
28
|
|
|
currentX += encoding.width; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
prepareSVG(){ |
|
33
|
|
|
// Clear the SVG |
|
34
|
|
|
while (this.svg.firstChild) { |
|
35
|
|
|
this.svg.removeChild(this.firstChild); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
calculateEncodingAttributes(this.encodings, this.options); |
|
39
|
|
|
|
|
40
|
|
|
var totalWidth = getTotalWidthOfEncodings(this.encodings); |
|
41
|
|
|
var maxHeight = getMaximumHeightOfEncodings(this.encodings); |
|
42
|
|
|
|
|
43
|
|
|
var width = totalWidth + this.options.marginLeft + this.options.marginRight; |
|
44
|
|
|
this.setSvgAttributes(width, maxHeight); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
drawSvgBarcode(parent, options, encoding){ |
|
48
|
|
|
var binary = encoding.data; |
|
49
|
|
|
|
|
50
|
|
|
// Creates the barcode out of the encoded binary |
|
51
|
|
|
var yFrom; |
|
52
|
|
|
if(options.textPosition == "top"){ |
|
53
|
|
|
yFrom = options.fontSize + options.textMargin; |
|
54
|
|
|
} |
|
55
|
|
|
else{ |
|
56
|
|
|
yFrom = 0; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
var barWidth = 0; |
|
60
|
|
|
var x; |
|
61
|
|
|
for(var b = 0; b < binary.length; b++){ |
|
62
|
|
|
x = b * options.width + encoding.barcodePadding; |
|
63
|
|
|
|
|
64
|
|
|
if(binary[b] === "1"){ |
|
65
|
|
|
barWidth++; |
|
66
|
|
|
} |
|
67
|
|
|
else if(barWidth > 0){ |
|
68
|
|
|
drawLine(x - options.width * barWidth, yFrom, options.width * barWidth, options.height, parent); |
|
69
|
|
|
barWidth = 0; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Last draw is needed since the barcode ends with 1 |
|
74
|
|
|
if(barWidth > 0){ |
|
75
|
|
|
drawLine(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height, parent); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
drawSVGText(parent, options, encoding){ |
|
80
|
|
|
var textElem = document.createElementNS(svgns, 'text'); |
|
81
|
|
|
|
|
82
|
|
|
// Draw the text if displayValue is set |
|
83
|
|
|
if(options.displayValue){ |
|
84
|
|
|
var x, y; |
|
85
|
|
|
|
|
86
|
|
|
textElem.setAttribute("style", |
|
87
|
|
|
"font:" + options.fontOptions + " " + options.fontSize + "px " + options.font |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
if(options.textPosition == "top"){ |
|
91
|
|
|
y = options.fontSize - options.textMargin; |
|
92
|
|
|
} |
|
93
|
|
|
else{ |
|
94
|
|
|
y = options.height + options.textMargin + options.fontSize; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Draw the text in the correct X depending on the textAlign option |
|
98
|
|
|
if(options.textAlign == "left" || encoding.barcodePadding > 0){ |
|
99
|
|
|
x = 0; |
|
100
|
|
|
textElem.setAttribute("text-anchor", "start"); |
|
101
|
|
|
} |
|
102
|
|
|
else if(options.textAlign == "right"){ |
|
103
|
|
|
x = encoding.width - 1; |
|
104
|
|
|
textElem.setAttribute("text-anchor", "end"); |
|
105
|
|
|
} |
|
106
|
|
|
// In all other cases, center the text |
|
107
|
|
|
else{ |
|
108
|
|
|
x = encoding.width / 2; |
|
109
|
|
|
textElem.setAttribute("text-anchor", "middle"); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
textElem.setAttribute("x", x); |
|
113
|
|
|
textElem.setAttribute("y", y); |
|
114
|
|
|
|
|
115
|
|
|
textElem.appendChild(document.createTextNode(encoding.text)); |
|
116
|
|
|
|
|
117
|
|
|
parent.appendChild(textElem); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
setSvgAttributes(width, height){ |
|
123
|
|
|
var svg = this.svg; |
|
124
|
|
|
svg.setAttribute("width", width + "px"); |
|
125
|
|
|
svg.setAttribute("height", height + "px"); |
|
126
|
|
|
svg.setAttribute("x", "0px"); |
|
127
|
|
|
svg.setAttribute("y", "0px"); |
|
128
|
|
|
svg.setAttribute("viewBox", "0 0 " + width + " " + height); |
|
129
|
|
|
|
|
130
|
|
|
svg.setAttribute("xmlns", svgns); |
|
131
|
|
|
svg.setAttribute("version", "1.1"); |
|
132
|
|
|
|
|
133
|
|
|
svg.style.transform = "translate(0,0)"; |
|
134
|
|
|
|
|
135
|
|
|
if(this.options.background){ |
|
136
|
|
|
svg.style.background = this.options.background; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
function createGroup(x, y, parent){ |
|
144
|
|
|
var group = document.createElementNS(svgns, 'g'); |
|
145
|
|
|
|
|
146
|
|
|
group.setAttribute("transform", "translate(" + x + ", " + y + ")"); |
|
147
|
|
|
|
|
148
|
|
|
parent.appendChild(group); |
|
149
|
|
|
|
|
150
|
|
|
return group; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
function setGroupOptions(group, options){ |
|
154
|
|
|
group.setAttribute("style", |
|
155
|
|
|
"fill:" + options.lineColor + ";" |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
function drawLine(x, y, width, height, parent){ |
|
160
|
|
|
var line = document.createElementNS(svgns, 'rect'); |
|
161
|
|
|
|
|
162
|
|
|
line.setAttribute("x", x); |
|
163
|
|
|
line.setAttribute("y", y); |
|
164
|
|
|
line.setAttribute("width", width); |
|
165
|
|
|
line.setAttribute("height", height); |
|
166
|
|
|
|
|
167
|
|
|
parent.appendChild(line); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
export default SVGRenderer; |
|
171
|
|
|
|